//
//  ViewController.m
//  ShiHuo
//
//  Created by zhiyou on 16-4-11.
//  Copyright (c) 2016年 wangyang. All rights reserved.
//

#import "ViewController.h"
#import "MainViewController.h"
#define USER_DEFAULTS [NSUserDefaults standardUserDefaults]
#define Height [UIScreen mainScreen].bounds.size.height
#define Width [UIScreen mainScreen].bounds.size.width
@interface ViewController () <UIScrollViewDelegate>
{
    UIScrollView    *_scrollView;
    UIPageControl   *_pageControl;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    
    if (![USER_DEFAULTS boolForKey:@"firstLaunch"]) {
        [USER_DEFAULTS setBool:YES forKey:@"firstLaunch"];
        [self firstLaunch];
    }else{
        [self notFirstLaunch];
    }
}
#pragma mark -- 第一次进入APP
- (void)firstLaunch {
    
    NSArray *arr = @[@"intro_img0",@"intro_img1",@"intro_img2",@"intro_img3"];
    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    for (int i = 0 ; i < arr.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(Width * i, 0, Width, Height)];
        imageView.image = [UIImage imageNamed:arr[i]];
        
        [_scrollView addSubview:imageView];
        if (i == 3)
        {
            UIButton *button = [self button];
            imageView.userInteractionEnabled = YES;
            [imageView addSubview:button];
        }
    }
    _scrollView.contentSize = CGSizeMake(Width*arr.count, Height);
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.directionalLockEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    [self.view addSubview:_scrollView];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, Height-100, Width, 20)];
    _pageControl.numberOfPages = arr.count;
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    [_pageControl addTarget:self action:@selector(changeImage) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_pageControl];
    
}
- (void)changeImage {
    
    int currentPage = (int)_pageControl.currentPage;
    
    // 计算当前scrollView所在的偏移量
    float contentOffSetX = currentPage*self.view.frame.size.width;
    
    // 设置scrollView的偏移量
    
    [_scrollView setContentOffset:CGPointMake(contentOffSetX, 0) animated:YES];
}
#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 获取滚动视图x轴偏移量
    float contentOffsetX = scrollView.contentOffset.x;
    // 计算当前在第几页
    int currentPage = contentOffsetX/320;
    // 设置pageControl当前所在的点
    _pageControl.currentPage = currentPage;
}
#pragma mark -- 不是第一次进入APP
- (void)notFirstLaunch {

    UIImageView *bgImage = [[UIImageView alloc] initWithFrame:self.view.frame];
    bgImage.image = [UIImage imageNamed:@"lunch"];
    [self.view addSubview:bgImage];
    
    [self performSelector:@selector(goToNext) withObject:nil afterDelay:2];
}
- (void)goToNext {
    
    MainViewController *vc = [[MainViewController alloc] init];
    [self.navigationController pushViewController:vc animated:NO];
  
}
- (UIButton *)button
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.bounds = CGRectMake(0, 0, Width*0.5, Height * 0.1);
    button.center = CGPointMake(Width/2, Height * 5/8);
    [button addTarget:self action:@selector(goToNext) forControlEvents:UIControlEventTouchUpInside];
    return button;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
